package sf.database.jdbc.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 通用java类型
 */
public abstract class ObjectType implements TypeHandler<Object> {
    private int sqlType;

    public Object get(ResultSet rs, String colName) throws SQLException {
        return rs.getObject(colName);
    }

    public Object get(ResultSet rs, int index) throws SQLException {
        return rs.getObject(index);
    }

    public Object get(CallableStatement rs, int index) throws SQLException {
        return rs.getObject(index);
    }

    @Override
    public Object get(CallableStatement cs, String parameterName) throws SQLException {
        return cs.getObject(parameterName);
    }

    @Override
    public void update(ResultSet rs, String columnLabel, Object value) throws SQLException {
        rs.updateObject(columnLabel, value);
    }

    @Override
    public void update(ResultSet rs, int columnIndex, Object value) throws SQLException {
        rs.updateObject(columnIndex, value);
    }

    public Object set(PreparedStatement ps, Object value, int i) throws SQLException {
        if (null == value) {
            ps.setNull(i, getSqlType());
        } else {
            ps.setObject(i, value);
        }
        return null;
    }

    @Override
    public Object set(CallableStatement cs, Object obj, String parameterName) throws SQLException {
        if (null == obj) {
            cs.setNull(parameterName, getSqlType());
        } else {
            cs.setObject(parameterName, obj);
        }
        return null;
    }

    @Override
    public int getSqlType() {
        return sqlType;
    }

    public void setSqlType(int sqlType) {
        this.sqlType = sqlType;
    }

    @Override
    public Class<Object> getDefaultJavaType() {
        return Object.class;
    }
}
